package com.infolab.aims.common;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.Validator;

import com.infolab.aims.GlobalConstant;
import com.infolab.aims.common.bean.AbstractForm;
import com.infolab.aims.common.flash.FlashMap;
import com.infolab.aims.domain.Department;
import com.infolab.aims.domain.Section;
import com.infolab.aims.domain.SystemAccessRight;
import com.infolab.aims.domain.SystemRole;
import com.infolab.aims.domain.SystemUser;
import com.infolab.aims.service.intf.CommonService;

public class AbstractController  {
	
	@Autowired
	private ApplicationContext context;
	
	@Autowired
	protected CommonService commonService;
	
	@Autowired
	protected Validator validator;
	
	Log logger = LogFactory.getLog(getClass());
	
	public CommonService getCommonService() {
		return commonService;
	}

	public void setCommonDropDownService(CommonService commonService) {
		this.commonService = commonService;
	}

	protected boolean isUpdateFormMode(AbstractForm form)
	{
		
		if(form==null) return false;
		return StringUtils.equalsIgnoreCase(GlobalConstant.FORM_MODE_UPDATE, form.getFormMode());
	}
	
	protected boolean isCreateFormMode(AbstractForm form)
	{
		if(form==null) return false;
		return StringUtils.equalsIgnoreCase(GlobalConstant.FORM_MODE_CREATE, form.getFormMode());
	}
	
	protected boolean isDeleteFormMode(AbstractForm form)
	{
		if(form==null) return false;
		return StringUtils.equalsIgnoreCase(GlobalConstant.FORM_MODE_DELETE, form.getFormMode());
	}

	public ApplicationContext getContext() {
		return context;
	}

	public void setContext(ApplicationContext context) {
		this.context = context;
	}
	
	public  void setInfoMessage(String info) {
		FlashMap.setInfoMessage(info);
	}

	public  void setWarningMessage(String warning) {
		FlashMap.setWarningMessage(warning);
	}

	public  void setErrorMessage(String error) {
		FlashMap.setErrorMessage(error);
	}

	public static void setSuccessMessage(String success) {
		FlashMap.setSuccessMessage(success);
	}
	
	public static void setSuccessMessage(HttpServletRequest request,String success) {
		FlashMap.setSuccessMessage(request,success);
	}
	
	public String getMessageResources(String key)
	{
		return context.getMessage(key, null, Locale.getDefault());
	}
	
	public String getMessageResources(String key,String arg0)
	{
		return context.getMessage(key, new String[]{arg0}, Locale.getDefault());
	}
	
	public String getMessageResources(String key,String arg0,String arg1)
	{
		return context.getMessage(key, new String[]{arg0, arg1}, Locale.getDefault());
	}

	public Validator getValidator() {
		return validator;
	}

	public void setValidator(Validator validator) {
		this.validator = validator;
	}
	
	public String getCurrentUserName(HttpServletRequest request)
	{
		SystemUser systemUser = (SystemUser) request.getSession().getAttribute(GlobalConstant.SESSION_CURRENT_SYSTEMUSER);
		if(systemUser!=null)
		{
			return systemUser.getUserName();
		}
		return null;
	}
	
	public Department getCurrentDepartment(HttpServletRequest request)
	{
		SystemUser systemUser = (SystemUser) request.getSession().getAttribute(GlobalConstant.SESSION_CURRENT_SYSTEMUSER);
		if(systemUser.getDepartment()!=null)
		{
			return systemUser.getDepartment();
		}
		return null;
	}
	
	public Section getCurrentSection(HttpServletRequest request)
	{
		SystemUser systemUser = (SystemUser) request.getSession().getAttribute(GlobalConstant.SESSION_CURRENT_SYSTEMUSER);
		if(systemUser.getSection()!=null)
		{
			return systemUser.getSection();
		}
		return null;
	}
	
	public String getCurrentSystemPersonName(HttpServletRequest request)
	{
		SystemUser systemUser = (SystemUser) request.getSession().getAttribute(GlobalConstant.SESSION_CURRENT_SYSTEMUSER);
		if(systemUser!=null)
		{
			return systemUser.getPersonName();
		}
		return null;
	}
	
	public void refreshGlobalSection(HttpServletRequest request)
	{
    	List list = commonService.getSection();
    	request.getSession().getServletContext().setAttribute(GlobalConstant.GLOBAL_SECTION, list);

    	StringWriter writer = new StringWriter();
    	ObjectMapper mapper = new ObjectMapper();
        JsonGenerator jsonGenerator;
		try {
			jsonGenerator = mapper.getJsonFactory().createJsonGenerator(writer);
			mapper.writeValue(jsonGenerator, list);
			request.getSession().setAttribute(GlobalConstant.GLOBAL_SECTION_JSON, writer);
		} catch (IOException e) {
			logger.error(CommonUtil.getStackTrace(e));
		}
	}
	
	public void refreshGlobalRole(HttpServletRequest request)
	{
    	List list = commonService.getRole();
    	request.getSession().getServletContext().setAttribute(GlobalConstant.GLOBAL_ROLE, list);
    	
    	StringWriter writer = new StringWriter();
    	ObjectMapper mapper = new ObjectMapper();
        JsonGenerator jsonGenerator;
		try {
			jsonGenerator = mapper.getJsonFactory().createJsonGenerator(writer);
			mapper.writeValue(jsonGenerator, list);
			request.getSession().setAttribute(GlobalConstant.GLOBAL_ROLE_JSON, writer);
		} catch (IOException e) {
			logger.error(CommonUtil.getStackTrace(e));
		}
	}

	public void refreshGlobalDepartment(HttpServletRequest request)
	{
    	List list = commonService.getDepartment();
    	request.getSession().getServletContext().setAttribute(GlobalConstant.GLOBAL_DEPARTMENT, list);
    	
	}
	
	public void refreshSystemCode(HttpServletRequest request,String systemCodeTypeCode)
	{
    	//List list = commonService.getSystemCodeValue(systemCodeTypeCode);
	}
	
	public Department findDepartment(HttpServletRequest request,Integer deptId)
	{
		List list = (List) request.getSession().getServletContext().getAttribute(GlobalConstant.GLOBAL_DEPARTMENT);
		if(list==null) return null;
		
		for(int i=0;i<list.size();i++)
		{
			Department department = (Department) list.get(i);
			if(department.getDepartmentId().equals(deptId))
				return department;
		}
		return null;
	}
	
	public Section findSection(HttpServletRequest request,Integer secId)
	{
		List list = (List) request.getSession().getServletContext().getAttribute(GlobalConstant.GLOBAL_SECTION);
		if(list==null) return null;
		
		for(int i=0;i<list.size();i++)
		{
			Section section = (Section) list.get(i);
			if(section.getSectionId().equals(secId))
				return section;
		}
		return null;
	}
	
	public SystemRole findSystemRole(HttpServletRequest request,Integer systemRoleId)
	{
		List list = (List) request.getSession().getServletContext().getAttribute(GlobalConstant.GLOBAL_ROLE);
		if(list==null) return null;
		
		for(int i=0;i<list.size();i++)
		{
			SystemRole systemRole = (SystemRole) list.get(i);
			if(systemRole.getSystemRoleId().equals(systemRoleId))
				return systemRole;
		}
		return null;
	}

	public SystemAccessRight findAccessRights(HttpServletRequest request,String accessRightId)
	{
		List list = (List) request.getSession().getServletContext().getAttribute(GlobalConstant.GLOBAL_ACCESS_RIGHT);
		if(list==null) return null;
		
		for(int i=0;i<list.size();i++)
		{
			SystemAccessRight systemAccessRight = (SystemAccessRight) list.get(i);
			if(systemAccessRight.getAccessRightId().equals(accessRightId))
				return systemAccessRight;
		}
		return null;
	}

	
	public boolean checkNullEmpty(Object obj){
		
		boolean flag=true;
		if(obj!=null && !obj.equals("")){
			flag = false;
		}
		return flag;
	
	
	}
}
